package com.halixun.d2app.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.halixun.d2app.bean.ClientUserInfo;
import com.halixun.d2app.net.DataState;

/**
 * @ProjectName: D2App
 * @Package: com.halixun.d2app.utils
 * @ClassName: DataHandle
 * @Description: java类作用描述
 * @Author: hdx
 * @CreateDate: 2019/11/28 18:44
 * @UpdateUser: hdx
 * @UpdateDate: 2019/11/28 18:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DataHandle {
    public static final String KEY_IS_LOGIN = DataHandle.class.getSimpleName() + "---key_app_is_login";

    public static final String KEY_NATIVE_COMMODITY = "key_native_commodity";

    public static final String KEY_FROM_DATA_TYPE = "key_from_data_type";

    public static SharedPreferences share(Context context) {
        return context.getSharedPreferences("shangYouYunData", Context.MODE_PRIVATE);
    }

    @SuppressLint("ApplySharedPref")
    public static void setNativeInfo(Context context, String json){
        share(context).edit().putString(KEY_NATIVE_COMMODITY,json).commit();
    }

    public static String getNativeInfo(Context context){
        return share(context).getString(KEY_NATIVE_COMMODITY,"");
    }

    public static boolean getDataFromType(Context context){
        return share(context).getBoolean(KEY_FROM_DATA_TYPE,true);
    }

    @SuppressLint({"CommitPrefEdits", "ApplySharedPref"})
    public static void setDataFromType(Context context , boolean isFromNet){
        share(context).edit().putBoolean(KEY_FROM_DATA_TYPE, isFromNet).commit();
    }




    /**
     * 在账号密码登陆或验证码登陆时保存数据
     * private String id ="";
     * private String code;
     * private String userphone;
     * private String password;
     * private String realname;
     * private String wechatno;
     * private String pid;
     * private String levelId;
     * private String levelName;
     * private String address;
     * private String idcard;
     * private String idcardFront;
     * private String idcardReverse;
     * private int buyResults;
     * private int reward;
     * private String headImg;
     * private int status;
     * private String ctime;
     *
     * @param context
     * @param info
     */
    @SuppressLint("ApplySharedPref")
    public static void appLogin(Context context, ClientUserInfo info) {
        SharedPreferences sharedPreferences = share(context);
        SharedPreferences.Editor ed = sharedPreferences.edit();
        ed.putBoolean(KEY_IS_LOGIN, true);
        ed.putInt("app_SiteUserId",info.getSiteUserId());
        ed.putInt("app_SiteId",info.getSiteId());
        if (!TextUtils.isEmpty(info.getSiteName())){
            ed.putString("app_SiteName",info.getSiteName());
        }
        ed.putInt("app_ShopId",info.getShopId());
        if (!TextUtils.isEmpty(info.getSiteUserName())){
            ed.putString("app_SiteUserName",info.getSiteUserName());
        }
        if (!TextUtils.isEmpty(info.getShopName())){
            ed.putString("app_ShopName",info.getSiteName());
        }
        ed.putInt("app_Seats",info.getSeats());
        if (!TextUtils.isEmpty(info.getAddress())){
            ed.putString("app_Address",info.getAddress());
        }
        if (!TextUtils.isEmpty(info.getLoginPassword())){
            ed.putString("app_LoginPassword",info.getLoginPassword());
        }
        if (!TextUtils.isEmpty(info.getTelephone())){
            ed.putString("app_Telephone",info.getTelephone());
        }
        if (!TextUtils.isEmpty(info.getGroupHead())){
            ed.putString("app_GroupHead",info.getGroupHead());
        }
        if (!TextUtils.isEmpty(info.getLogTime())){
            ed.putString("app_LogTime",info.getLogTime());
        }
        ed.putBoolean("app_IsTableTimeFee",info.isIsTableTimeFee());
        ed.putInt("app_TableTimeFeeType",info.getTableTimeFeeType());
        ed.putBoolean("app_IsOpenHuaBei",info.isIsOpenHuaBei());
        ed.putFloat("app_HuaBeiMinMoney",info.getHuaBeiMinMoney());
        if (!TextUtils.isEmpty(info.getAgentName())){
            ed.putString("app_AgentName",info.getAgentName());
        }
        if (!TextUtils.isEmpty(info.getAgentPhone())){
            ed.putString("app_AgentPhone",info.getAgentPhone());
        }
        ed.putInt("app_AgentId",info.getAgentId());
        ed.commit();
    }


    /**
     * app在启动时数据还原操作
     *
     * @param context
     */
    public static void appEnter(Context context) {
        SharedPreferences sharedPreferences = share(context);
        DataState.getInstance().setLoginState(sharedPreferences.getBoolean(KEY_IS_LOGIN, false));
//        DataState.getInstance().setGetDataFromNet(sharedPreferences.getBoolean(KEY_FROM_DATA_TYPE, true));
        if (DataState.getInstance().isLoginState()) {
            ClientUserInfo info = new ClientUserInfo();
            info.setSiteUserId(sharedPreferences.getInt("app_SiteUserId",0));
            info.setSiteId(sharedPreferences.getInt("app_SiteId",0));
            info.setSiteName(sharedPreferences.getString("app_SiteName",""));
            info.setShopId(sharedPreferences.getInt("app_ShopId",0));
            info.setSiteUserName(sharedPreferences.getString("app_SiteUserName",""));
            info.setShopName(sharedPreferences.getString("app_ShopName",""));
            info.setSeats(sharedPreferences.getInt("app_Seats",0));
            info.setAddress(sharedPreferences.getString("app_Address",""));
            info.setLoginPassword(sharedPreferences.getString("app_LoginPassword",""));
            info.setTelephone(sharedPreferences.getString("app_Telephone",""));
            info.setGroupHead(sharedPreferences.getString("app_GroupHead",""));

            info.setLogTime(sharedPreferences.getString("app_LogTime",""));
            info.setIsTableTimeFee(sharedPreferences.getBoolean("app_IsTableTimeFee",false));
            info.setTableTimeFeeType(sharedPreferences.getInt("app_TableTimeFeeType",0));
            info.setIsOpenHuaBei(sharedPreferences.getBoolean("app_IsOpenHuaBei",false));
            info.setHuaBeiMinMoney(sharedPreferences.getFloat("app_HuaBeiMinMoney",0));
            info.setAgentName(sharedPreferences.getString("app_AgentName",""));
            info.setAgentPhone(sharedPreferences.getString("app_AgentPhone",""));
            info.setAgentId(sharedPreferences.getInt("app_AgentId",0));
            DataState.getInstance().setClientUserInfo(info);
        }
    }


    /**
     * app在用户主动点击退出或切换账号时调用（下次调用时必定为未登录状态）
     *
     * @param context
     */
    public static void appExit(Context context) {
        SharedPreferences sharedPreferences = share(context);
        SharedPreferences.Editor ed = sharedPreferences.edit();
        ed.clear();
        ed.apply();
    }
}
